import api from "../../utils/apiInstance"
import {
  groupBy
} from '../../utils/util'
const app = getApp();
Page({
  data: {
    baseUrl: app.globalData.baseUrl,
    fileUrl: app.globalData.fileUrl,
    tabListConst: [{
        name: '生效中',
        tag: "",
        params: {
          status: '1'
        }
      },
      {
        name: '审核中',
        tag: "",
        params: {
          status: '2'
        }
      },
      {
        name: '已过期',
        tag: "",
        params: {
          status: '3'
        }
      },
      {
        name: '不通过',
        tag: "",
        params: {
          status: '4'
        }
      },
    ],
    // tabListConst:[
    //   {name:'全部', tag:"" , params:{status:'all'}},
    //   {name:'使用中', tag:"", params:{status:'Normal'}},
    //   {name:'快过期', tag:"", params:{status:'ExpireImmediately'}},
    //   {name:'已过期', tag:"" , params:{status:'Expired|Cancel'}},
    //   {name:'审核', tag:"", params:{status:'PendingReview|NotPassReview'}}
    // ],
    tabList: [],
    tabIndex: 0,
    tabStatus: "all",
    cardList: {}, //对象列表
    showCardList: [] //显示列表
  },
  async onLoad() {
    this.loadData(1)
  },

  // async loadData(){
  //   const cardList = await api.get(`/v1/openApi/certificate/owner/search/${wx.getStorageSync('userinfo').siocSapNo}`);
  //   const tempArray = groupBy(cardList.rows,item=>item.status)
  //   let  tempCardList = {};
  //   //构造tab
  //   const tempTabList = [{name:'全部', tag:`[${cardList.rows.length}]`, params:{status:'all'}}];
  //   tempCardList['all'] =cardList.rows
  //   for(let i in tempArray){
  //     const tabTarget = this.data.tabListConst.find(item=>{
  //       return item.params.status.split("|").includes(i)
  //     })
  //     tempTabList.push({
  //       name:tabTarget.name,
  //       tag:`[${tempArray[i].length}]`,
  //       params:tabTarget.params
  //     })
  //   }
  //   tempCardList = {...tempCardList, ...tempArray}
  //   this.setData({
  //     tabList:tempTabList,
  //     cardList:tempCardList,
  //     showCardList:tempCardList['all']
  //   })
  //   console.log("tempCardList",tempCardList);
  // },
  async loadData(status) {
    const cardList = await api.get(`/v1/openApi/certificate/owner/search/${wx.getStorageSync('userinfo').siocSapNo}`, {status:status});
    this.setData({
      showCardList: cardList.data
    })
  },

  //换证
  changeCerti(params) {
    console.log('id', params.detail);
    wx.navigateTo({
      url: `/subPages/zhengJianGlForm/zhengJianGlForm?id=${params.detail.id}`,
    })
  },

  mergeReCall(params) {
    wx.navigateTo({
      url: `/subPages/zhengJianGlForm/zhengJianGlForm?id=${params.detail.id}&type=3`,
    })
  },


  // tabChange(e){
  //   console.log(e);
  //   let tempArray = [];
  //   if(e.detail.params.status){

  //     if(e.detail.params.status.indexOf("|")>0){


  //       let statusList = e.detail.params.status.split('|')
  //       console.log("statusList",statusList)
  //       statusList.forEach(item => {
  //           this.data.cardList[item]&&tempArray.push(...this.data.cardList[item])
  //       });

  //     }
  //     else{
  //       tempArray = this.data.cardList[e.detail.params.status]
  //     }
  //   }

  //   this.setData({
  //     tabIndex:e.detail.tabIndex,
  //     tabStatus:e.detail.params.status,
  //     showCardList:tempArray
  //   })
  //   console.log('e',e);

  // },

  tabChange(e) {
    const status = e.detail.params.status;
    this.loadData(status);
  },

  doAdd() {
    wx.navigateTo({
      url: `/subPages/zhengJianGlForm/zhengJianGlForm`,
    })
  }

})